import os
import time
from datetime import datetime
import pytest
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep

from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


@pytest.fixture(scope="function")
def driver():
    service = Service(
        #提交最终代码脚本时，请将驱动路径换回官方路径"C:\\Users\\86153\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"
        executable_path="D:\\Software\\Chrome\\Driver\\chromedriver.exe")
    driver = webdriver.Chrome(service=service)
    driver.get("https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc")
    driver.maximize_window()
    yield driver
    driver.quit()


class Test12306:
    # 组件定位
    __SIGNAL_TRAVEL = (By.XPATH, "/html/body/div[2]/div[7]/div[9]/form/div[1]/ul/li[1]/input")
    __ROUND_TRAVEL  = (By.XPATH, "/html/body/div[2]/div[7]/div[9]/form/div[1]/ul/li[2]/input")
    __LEAVE_PLACE_INPUT = (By.XPATH,"/html/body/div[2]/div[7]/div[9]/form/div[2]/ul/li[1]/div/input[2]")
    __TARGET_PLACE_SELECTOR = (By.XPATH,"/html/body/div[2]/div[9]/div/div[2]/div[1]")
    __ARRIVE_PLACE_INPUT = (By.XPATH,"/html/body/div[2]/div[7]/div[9]/form/div[2]/ul/li[3]/div/input[2]")
    __LEAVE_DATA_INPUT = (By.XPATH,"/html/body/div[2]/div[7]/div[9]/form/div[2]/ul/li[4]/div/input")
    __ROUND_DATA_INPUT = (By.XPATH,"/html/body/div[2]/div[7]/div[9]/form/div[2]/ul/li[5]/div/input")
    __QUERY_BUTTON = (By.ID,"query_ticket")
    __RESULT_LIST = (By.XPATH,"/html/body/div[3]/div[7]/div[13]/table/tbody")
    __NO_RESULT_HINT = (By.XPATH,"//p[contains(text(), '很抱歉，按您的查询条件，当前未找到从')]")
    __ERROR_HINT = (By.XPATH,"//p[contains(text(),'查询超时，请稍后再试！')]")
    __WARN_HINT = (By.XPATH,"/html/body/div[7]")
    __STUDENT_SELECTOR = (By.XPATH,"/html/body/div[2]/div[7]/div[9]/form/div[3]/ul/li[2]/input")
    __DISCOUNT_SELECTOR = (By.XPATH,"/html/body/div[2]/div[7]/div[11]/span/input[1]")
    __INTERVAL_LEAVE_PLACE_INPUT = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[1]/div/input[2]")
    __INTERVAL_ARRIVE_PLACE_INPUT = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[2]/div/input[2]")
    __INTERVAL_USE_MID_STATION = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[4]/span/input")
    __INTERVAL_MID_PLACE_INPUT = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[4]/div/input[2]")
    __INTERVAL_LEVAL_DATA_INPUT = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[3]/div/input")
    __INTERVAL_QUERY_BUTTON = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/div[1]/a")
    __INTERVAL_TARGET_PLACE_SELECTOR = (By.XPATH,"/html/body/div[1]/div[4]/div/div[2]/div[1]")
    __INTERVAL_HAVE_SITE = (By.XPATH,"/html/body/div[1]/div[2]/div[2]/div/ul/li[5]/input")

    # 行为
    # 最长的强制等待时间
    timeout = 10

    # 自定义封装查询组件函数，包含指定时间的显示等待
    def find_element(self,driver,by,value):
        try:
            WebDriverWait(driver, self.timeout).until(
                expected_conditions.visibility_of_element_located((by, value))
            )
            return WebDriverWait(driver, self.timeout).until(
                expected_conditions.element_to_be_clickable((by, value))
            )
        except Exception as e:
            print(f"元素定位失败！定位方式：{by}，元素路径：{value}，错误详情：{type(e).__name__}: {str(e)}")
            return None


    #  请在此处插入python+selenium代码
    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data",
        [
            ["北京","广州","2025-11-01"],
            ["北京","成都","2025-11-01"],
            ["上海","广州","2025-11-01"],
            ["上海","成都","2025-11-01"]
        ],ids=["12306_R001_001","12306_R001_002","12306_R001_003","12306_R001_004"]
    )
    def test_12306_R001(self,driver,request,leave_place,arrive_place,leave_data):
        """
        在12306查询从指定出发地到指定目的地的单程车票
        :param driver:驱动
        :param request:pytest测试记录
        :param leave_place:出发地
        :param arrive_place:目的地
        :param leave_data:出发日期
        :return:无
        """
        print(f"{leave_place} --> {arrive_place}")
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击单程单选框 确保选中
            self.find_element(driver, *self.__SIGNAL_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
            arguments[0].value = arguments[1];        // 赋值日期
            """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)

            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            tbody = self.find_element(driver,*self.__RESULT_LIST)
            if not tbody:
                no_result_hint = self.find_element(driver,*self.__NO_RESULT_HINT)
                if not no_result_hint:
                    status = False
                    msg = "其他错误：介于无结果和有结果但无法查看"
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)


    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data",
        [
            ["北京","广州","2025-09-01"],
            ["上海","成都","2025-09-01"]
        ],ids=["12306_R002_001","12306_R002_002"]
    )
    def test_12306_R002(self,driver,request,leave_place,arrive_place,leave_data):
        """
        测试输入无效日期是否有提示
        :param driver: 驱动
        :param request: pytest测试记录
        :param leave_place: 出发地
        :param arrive_place: 到达地
        :param leave_data: 出发时间
        :return:
        """
        print(f"{leave_place} --> {arrive_place}")
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击单程单选框 确保选中
            self.find_element(driver, *self.__SIGNAL_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
                    arguments[0].value = arguments[1];        // 赋值日期
                    """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)

            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            error_hint = self.find_element(driver,*self.__ERROR_HINT)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)



    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data,train_type,leave_station,arrive_station,site_type",
        [
            ["北京","广州","2025-11-01","高铁","丰台","白云","二等座"],
            ["上海","成都","2025-11-01","高铁","虹桥","成都东","二等座"]
        ],ids=["12306_R003_001","12306_R003_002"]
    )
    def test_12306_R003(self,driver,request,leave_place,arrive_place,leave_data,train_type,leave_station,arrive_station,site_type):
        """
        用户能够通过筛选功能根据车次类型、出发车站、到达车站和车次席别正确筛选结果。
        :param driver: 驱动
        :param request: pytes请求
        :param leave_place: 出发地
        :param arrive_place: 目的地
        :param leave_data: 出发时间
        :param train_type: 车次类型
        :param leave_station: 出发站台
        :param arrive_station: 到达站台
        :param site_type: 车次席位
        :return: 无
        """
        global tbody
        print(f"{leave_place} --> {arrive_place}")
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击单程单选框 确保选中
            self.find_element(driver, *self.__SIGNAL_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
                arguments[0].value = arguments[1];        // 赋值日期
                """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)

            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            tbody = self.find_element(driver,*self.__RESULT_LIST)
            if tbody:
                self.find_element(driver,By.XPATH,f"//li[contains(., '{train_type}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{leave_station}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{arrive_station}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{site_type}')]//input").click()
            time.sleep(3)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status and tbody,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)



    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data,round_data",
        [
            ["北京","广州","2025-11-01","2025-11-07"],
            ["上海","成都","2025-11-01","2025-11-07"]
        ],ids=["12306_R004_001","12306_R004_002"]
    )
    def test_12306_R004(self,driver,request,leave_place,arrive_place,leave_data,round_data):
        """
        验证返程日期为预售期终止日期往返车票查询是否正确显示
        :param driver:驱动
        :param request:pytest测试记录
        :param leave_place:出发地
        :param arrive_place:到达地
        :param leave_data:出发时间
        :param round_data:返程时间
        :return:无
        """
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击返程单选框 确保选中
            self.find_element(driver, *self.__ROUND_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行与返程时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
                arguments[0].value = arguments[1];        // 赋值日期
                """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)
            round_data_input_ele = self.find_element(driver,*self.__ROUND_DATA_INPUT)
            driver.execute_script(js_script, round_data_input_ele, round_data)

            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            tbody = self.find_element(driver,*self.__RESULT_LIST)
            if not tbody:
                no_result_hint = self.find_element(driver,*self.__NO_RESULT_HINT)
                if not no_result_hint:
                    status = False
                    msg = "其他错误：介于无结果和有结果但无法查看"
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status ,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)


    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data,round_data",
        [
            ["北京","广州","2025-11-01","2025-10-07"],
            ["上海","成都","2025-11-01","2025-10-07"]
        ],ids=["12306_R005_001","12306_R005_002"]
    )
    def test_12306_R005(self,driver,request,leave_place,arrive_place,leave_data,round_data):
        """
        验证返程日在出发日之前往返车票查询是否正确显示
        :param driver:驱动
        :param request:pytest测试记录
        :param leave_place:出发地
        :param arrive_place:到达地
        :param leave_data:出发时间
        :param round_data:返程时间
        :return:无
        """
        global warn_hint
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击返程单选框 确保选中
            self.find_element(driver, *self.__ROUND_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行与返程时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
                arguments[0].value = arguments[1];        // 赋值日期
                """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)
            round_data_input_ele = self.find_element(driver,*self.__ROUND_DATA_INPUT)
            driver.execute_script(js_script, round_data_input_ele, round_data)

            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            # warn_hint = self.find_element(driver,*self.__WARN_HINT)
            time.sleep(2)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)


    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data,round_data,train_type,leave_station,arrive_station,site_type,is_student,is_discount",
        [
            ["北京","广州","2025-11-01","2025-11-07","高铁","丰台","白云","二等座",True,True],
            ["上海","成都","2025-11-01","2025-11-07","高铁","虹桥","成都东","二等座",True,False]
        ],ids=["12306_R006_001","12306_R006_002"]
    )
    def test_12306_R006(self,driver,request,leave_place,arrive_place,leave_data,round_data,train_type,leave_station,arrive_station,site_type,is_student,is_discount):
        """
        用户能够通过筛选功能根据车次类型、出发车站、到达车站、车次席别、学生票、折扣车次正确筛选结果。
        :param driver: 驱动
        :param request: pytes请求
        :param leave_place: 出发地
        :param arrive_place: 目的地
        :param leave_data: 出发时间
        :param round_data: 返程时间
        :param train_type: 车次类型
        :param leave_station: 出发站台
        :param arrive_station: 到达站台
        :param site_type: 车次席位
        :param is_student：查询学生票
        :param is_discount:查询折扣车票
        :return: 无
        """
        global tbody
        status = True
        msg = None
        try:
            actions = ActionChains(driver)

            # 单击返程单选框 确保选中
            self.find_element(driver, *self.__ROUND_TRAVEL).click()

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__TARGET_PLACE_SELECTOR).click()

            # 输入出行与返程时间
            leave_data_input_ele = self.find_element(driver,*self.__LEAVE_DATA_INPUT)
            js_script = """
                arguments[0].value = arguments[1];        // 赋值日期
                """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)
            round_data_input_ele = self.find_element(driver,*self.__ROUND_DATA_INPUT)
            driver.execute_script(js_script, round_data_input_ele, round_data)
            if is_student:
                self.find_element(driver,*self.__STUDENT_SELECTOR).click()
            if is_discount:
                self.find_element(driver,*self.__DISCOUNT_SELECTOR).click()
            # 点击查询按钮
            self.find_element(driver,*self.__QUERY_BUTTON).click()

            # 等待结果显示
            tbody = self.find_element(driver,*self.__RESULT_LIST)
            if tbody:
                self.find_element(driver,By.XPATH,f"//li[contains(., '{train_type}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{leave_station}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{arrive_station}')]//input").click()
                self.find_element(driver,By.XPATH,f"//li[contains(., '{site_type}')]//input").click()
            time.sleep(3)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status and tbody,f"测试失败，原因:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)


    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data",
        [
            ["哈尔滨","济南","2025-11-07"],
            ["哈尔滨","西安","2025-11-07"]
        ],ids=["12306_R007_001","12306_R007_002"]
    )
    def test_12306_R007(self,driver,request,leave_place,arrive_place,leave_data):
        """
        验证从哈尔滨到济南在2025-11-07等的车次列表是否正确显示
        :param driver: 驱动
        :param request: pytest请求
        :param leave_place: 出发地
        :param arrive_place: 到达地
        :param leave_data: 出发时间
        :return: 无
        """
        status = True
        msg = None
        try:
            driver.get("https://kyfw.12306.cn/otn/lcQuery/init")
            actions = ActionChains(driver)

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__INTERVAL_LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__INTERVAL_ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()

            # 输入出行时间
            leave_data_input_ele = self.find_element(driver,*self.__INTERVAL_LEVAL_DATA_INPUT)
            js_script = """
            arguments[0].value = arguments[1];        // 赋值日期
            """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)

            # 点击查询按钮
            self.find_element(driver,*self.__INTERVAL_QUERY_BUTTON).click()
            time.sleep(3)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status,f"Error,message:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)


    # @pytest.mark.parametrize(
    #     "leave_place,arrive_place,leave_data,mid_station",
    #     [
    #         ["哈尔滨","南京","2025-11-07","无效换乘站"]
    #     ],ids=["12306_R008_001"]
    # )
    # def test_12306_R008(self,driver,request,leave_place,arrive_place,leave_data,mid_station):
    #     """
    #     【说明】由于12306站点更新，现在在中转站点填写无效换乘站，其会自动匹配最接近的站点，因此该测试用例已无法测试
    #     """
    #     status = True
    #     msg = None
    #     try:
    #         driver.get("https://kyfw.12306.cn/otn/lcQuery/init")
    #         actions = ActionChains(driver)
    #
    #         # 输入出发地
    #         leave_input_ele = self.find_element(driver,*self.__INTERVAL_LEAVE_PLACE_INPUT)
    #         actions.click(leave_input_ele).send_keys(leave_place).perform()
    #         self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()
    #         arrive_input_ele = self.find_element(driver,*self.__INTERVAL_ARRIVE_PLACE_INPUT)
    #         actions.click(arrive_input_ele).send_keys(arrive_place).perform()
    #         self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()
    #
    #         # 输入出行时间
    #         leave_data_input_ele = self.find_element(driver,*self.__INTERVAL_LEVAL_DATA_INPUT)
    #         js_script = """
    #                 arguments[0].value = arguments[1];        // 赋值日期
    #                 """
    #         driver.execute_script(js_script, leave_data_input_ele, leave_data)
    #
    #         # 选择中转站点
    #         self.find_element(driver,*self.__INTERVAL_USE_MID_STATION).click()
    #         mid_station_input_ele = self.find_element(driver,*self.__INTERVAL_MID_PLACE_INPUT)
    #         actions.click(mid_station_input_ele).send_keys(mid_station).perform()
    #         self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()
    #
    #         # 点击查询按钮
    #         self.find_element(driver,*self.__INTERVAL_QUERY_BUTTON).click()
    #         time.sleep(3)
    #     except Exception as e:
    #         status = False
    #         msg = f"{type(e).__name__}: {str(e)}"
    #     assert status,f"Error,message:{msg}"
    #     case_id = request.node.callspec.id
    #     self.take_screenshot(driver,case_id)


    @pytest.mark.parametrize(
        "leave_place,arrive_place,leave_data,mid_station",
        [
            ["哈尔滨","南京","2025-11-07","周口"]
        ],ids=["12306_R009_001"]
    )
    def test_12306_R009(self,driver,request,leave_place,arrive_place,leave_data,mid_station):
        """
        验证从哈尔滨到南京在2025-11-07且换乘站为周口的车次列表是否正确显示
        :param driver:
        :param request:
        :param leave_place:
        :param arrive_place:
        :param leave_data:
        :param mid_station:
        :return:
        """
        status = True
        msg = None
        try:
            driver.get("https://kyfw.12306.cn/otn/lcQuery/init")
            actions = ActionChains(driver)

            # 输入出发地
            leave_input_ele = self.find_element(driver,*self.__INTERVAL_LEAVE_PLACE_INPUT)
            actions.click(leave_input_ele).send_keys(leave_place).perform()
            self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()
            arrive_input_ele = self.find_element(driver,*self.__INTERVAL_ARRIVE_PLACE_INPUT)
            actions.click(arrive_input_ele).send_keys(arrive_place).perform()
            self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()

            # 输入出行时间
            leave_data_input_ele = self.find_element(driver,*self.__INTERVAL_LEVAL_DATA_INPUT)
            js_script = """
                arguments[0].value = arguments[1];        // 赋值日期
                """
            driver.execute_script(js_script, leave_data_input_ele, leave_data)

            # 选择中转站点
            self.find_element(driver,*self.__INTERVAL_USE_MID_STATION).click()
            mid_station_input_ele = self.find_element(driver,*self.__INTERVAL_MID_PLACE_INPUT)
            actions.click(mid_station_input_ele).send_keys(mid_station).perform()
            self.find_element(driver,*self.__INTERVAL_TARGET_PLACE_SELECTOR).click()

            # 点击查询按钮
            self.find_element(driver,*self.__INTERVAL_QUERY_BUTTON).click()
            time.sleep(3)
        except Exception as e:
            status = False
            msg = f"{type(e).__name__}: {str(e)}"
        assert status,f"Error,message:{msg}"
        case_id = request.node.callspec.id
        self.take_screenshot(driver,case_id)



    #  test-code-end

    @staticmethod
    def take_screenshot(driver, file_name):
        timestamp = datetime.now().strftime("%H%M%S%d%f")
        timestamped_file_name = f"{timestamp}_{file_name}.png"
        screenshots_dir = "screenshots"
        if not os.path.exists(screenshots_dir):
            os.makedirs(screenshots_dir)
        screenshot_file_path = os.path.join(screenshots_dir, timestamped_file_name)
        driver.save_screenshot(screenshot_file_path)

